package com.miage.itrading.gui;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.miage.itrading.dao.CotationDAO;
import com.miage.itrading.utils.Util;
public class BarreProgressionChargement extends Thread
{
	private static final long serialVersionUID = 1L;


	public void run() {
		try {
			CotationDAO dao = new CotationDAO();
			
			ArrayList<Integer> ids;
			
			ids = (ArrayList<Integer>) dao.getAllIdsCotations();

			int nb = ids.size();
	
			JFrame frame = new JFrame("Chargement des données");
			JPanel panneau		= new JPanel();
			JProgressBar bar	= new JProgressBar(0, nb);
			JLabel text			= new JLabel("0 %");
			
			panneau.add(bar);
			panneau.add(text);
			
			
			frame.setSize(300, 100);
			panneau.setSize(300, 100);
			bar.setSize(250, bar.getHeight());
			frame.add(panneau, BorderLayout.CENTER);
			frame.setResizable(true);
			
			java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			
			frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2);
			
			text.setVisible(true);
			bar.setVisible(true);
			panneau.setVisible(true);
			frame.setVisible(true);
			
			for (int i=0 ; i<nb ; ++i) {
				Util.addCotationCache(dao.getCotationById(ids.get(i)), true);
				bar.setValue(i);
				
				text.setText((int) ((i +1) * 100 / nb)  + " %");
			}
			
			frame.removeAll();
			frame.setEnabled(false);
			frame.setVisible(false);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}